from rest_framework import serializers

from apps.article.models import Article
from apps.question.models import Label, Question, Reply
from apps.recruit.serializer import Enterprise_Serializer
from apps.user.models import User


class User_Serialzier(serializers.ModelSerializer):
    class Meta:
        # 指定模型类
        model = User
        # 指定字段
        fields = ('id', 'username', 'avatar')


class SubsSerialzier(serializers.ModelSerializer):
    user = User_Serialzier(read_only=True, many=True)
    class Meta:
        # 指定模型类
        model = Reply
        # 指定字段
        fields = ('id', 'content','createtime','unuseful_count', 'useful_count', 'user')


class Answer_QuestionSerialzier(serializers.ModelSerializer):
    subs = SubsSerialzier(read_only=True, many=True)
    user = User_Serialzier(read_only=True, many=True)
    class Meta:
        # 指定模型类
        model = Reply
        # 指定字段
        fields = '__all__'


class QuestionSerialzier(serializers.ModelSerializer):
    class Meta:
        # 指定模型类
        model = Question
        # 指定字段
        fields = ('id', 'createtime', 'labels', 'reply', 'replyname', 'replytime', 'title', 'unuseful_count', 'useful_count', 'user', 'visits')


class LabelSerialzier(serializers.ModelSerializer):
    class Meta:
        # 指定模型类
        model = Label
        # 指定字段
        fields = ('id', 'label_name')


class ArticlesSerialzier(serializers.ModelSerializer):
    class Meta:
        # 指定模型类
        model = User
        # 指定字段
        fields = ('id', 'title')


class User1_Serialzier(serializers.ModelSerializer):
    articles = ArticlesSerialzier(read_only=True, many=True)
    class Meta:
        # 指定模型类
        model = User
        # 指定字段
        fields = ('id', 'username', 'avatar', 'articles', 'fans')


class Collected_ArticlesSerialzier(serializers.ModelSerializer):
    collected = serializers.BooleanField(read_only=True, many=True)
    user = User1_Serialzier(read_only=True, many=True)
    class Meta:
        # 指定模型类
        model = Article
        # 指定字段
        fields = '__all__'


class UserSerialzier(serializers.ModelSerializer):
    labels = LabelSerialzier(read_only=True)
    questions = QuestionSerialzier(read_only=True)
    answer_question = Answer_QuestionSerialzier(read_only=True)
    collected_articles = Collected_ArticlesSerialzier(read_only=True)
    enterprises = Enterprise_Serializer(read_only=True)

    class Meta:
        # 指定模型类
        model = User
        # 指定字段
        fields = '__all__'


